
using System;
using System.Reflection;
using QdbUnit;
using QdbUnit.Extensions;
using Qdb.Foundation;

namespace QdbUnit.Extensions
{
	public class QdbTestSuiteBuilder : ReflectionTestSuiteBuilder
	{
		private IQdbFixture _fixture;

		public QdbTestSuiteBuilder(IQdbFixture fixture, Type clazz) : this(fixture, new 
			Type[] { clazz })
		{
		}

		public QdbTestSuiteBuilder(IQdbFixture fixture, Type[] classes) : base(classes)
		{
			Fixture(fixture);
		}

		private void Fixture(IQdbFixture fixture)
		{
			if (null == fixture)
			{
				throw new ArgumentNullException("fixture");
			}
			_fixture = fixture;
		}

		protected override bool IsApplicable(Type clazz)
		{
			return _fixture.Accept(clazz);
		}

		protected override ITest CreateTest(object instance, MethodInfo method)
		{
			ITest test = base.CreateTest(instance, method);
			return new _TestDecorationAdapter_38(test, test);
		}

		private sealed class _TestDecorationAdapter_38 : TestDecorationAdapter
		{
			public _TestDecorationAdapter_38(ITest test, ITest baseArg1) : base(baseArg1)
			{
				this.test = test;
			}

			public override string Label()
			{
				return "(" + QdbFixtureVariable.Fixture().Label() + ") " + test.Label();
			}

			private readonly ITest test;
		}

		protected override object WithContext(IClosure4 closure)
		{
			return QdbFixtureVariable.FixtureVariable.With(_fixture, closure);
		}
	}
}
